<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Framework_Controller_Web
 *
 * @author      Joe Stump <joe@joestump.net>
 * @copyright   (c) 2005 2006 Joseph C. Stump. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @package     Framework
 * @filesource
 */

/**
 * Framework_Controller_Web
 *
 * @author      Joe Stump <joe@joestump.net>
 * @copyright   (c) 2005 2006 Joseph C. Stump. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @package     Framework
 * @filesource
 */
class Framework_Controller_Web extends Framework_Controller_Common
{
	/**
	 * $requester
	 *
	 * @access      public
	 * @var         string      $requester      Framework_Request type to use
	 */
	public $requester = 'Web';



	/**
	 * module
	 *
	 * @access      public
	 * @param       object      $request
	 */
	public function module()
	{
		$module = Framework_Module::factory(Framework::$request);
		// Check for a cachable module
		if ($module->isCacheable && !count($_POST) && !isset($_GET['noCache'])) {
			$handler = $module->getCacheHandler();
			// Lets see if we get a cache hit
			$cache = Framework_Cache::factory($handler->getCache());
			if (!PEAR::isError($cache)) {
				// Got the cache now see if there is something there
				if($data = $cache->get($handler->getID(),$handler->getGroup())) {
					$module->templateCache = $data;
					if ($data['presenter']) {
						$module->presenter = $data['presenter'];
					} else {
						$module->presenter = 'Smarty';
					}
				}
			}
		}
			
		return $module;
			
			
	}



	/**
	 * authenticate
	 *
	 * @access      public
	 * @return      mixed
	 */
	public function authenticate()
	{
		try {

			$auth = Framework_Auth::factory(Framework::$module);

			if (PEAR::isError($auth)) {
				return PEAR::raiseError($result->getMessage,
				FRAMEWORK_ERROR_AUTH);
			}

			$result = $auth->authenticate();

			if (!is_bool($result) || $result !== true) {
				return PEAR::raiseError('Authentication failed',
				FRAMEWORK_ERROR_AUTH);
			}
		} catch (Framework_Exception $error) {
			return PEAR::raiseError($error->getMessage(),
			FRAMEWORK_ERROR_AUTH);
		}
	}

	/**
	 * start
	 *
	 * @access      public
	 * @return      mixed
	 */
	public function start()
	{
		return true;
	}

	/**
	 * run
	 *
	 * @access      protected
	 * @return      mixed
	 */
	protected function run()
	{
		$result = Framework_Module::start(Framework::$module, Framework::$request);

		if (PEAR::isError($result)) {
			return PEAR::raiseError($result->getMessage(),
			FRAMEWORK_ERROR_MODULE_EVENT);
		} else {
			Framework::$module->ran = true;
		}
	}

	/**
	 * display
	 *
	 * @access      public
	 * @return      void
	 */
	public function display()
	{
		
		
		
		
		// Normal Caching
		if (!Framework::$module->isCacheable) {
			$result = $this->run();
			if (PEAR::isError($result)) {
				return $result;
			}

			$presenter = Framework_Presenter::factory(Framework::$module->presenter, Framework::$module);
			if (!PEAR::isError($presenter)) {
				

					$presenter->display();

					return;

			}
		} else {

			if(Framework::$module->templateCache) {
				$presenter = Framework_Presenter::factory(Framework::$module->presenter, Framework::$module);
				if (!PEAR::isError($presenter)) {
					if ($presenter->cacheInit()) {
						
						$presenter->display();
					} else {
						// Run
						$result = $this->run();
						if (PEAR::isError($result)) {
							return $result;
						}

						// Cache
						$handler = Framework::$module->getCacheHandler();
						$cache = Framework_Cache::factory($handler->getCache());
						$cacheData = $handler->getDataToCache();
						$cache->save($cacheData,$handler->getID(),$handler->getGroup());
						Framework::$module->templateCache = $cacheData;



						// Display
						$presenter->display();




					}


				} else {
					return PEAR::raiseError($presenter->getMessage(),FRAMEWORK_ERROR_PRESENTER);
				}

			} else {
				// Run
				$result = $this->run();
				if (PEAR::isError($result)) {
					return $result;
				}


				// Cache

				$handler = Framework::$module->getCacheHandler();
				$cache = Framework_Cache::factory($handler->getCache());
				$cacheData = $handler->getDataToCache();
				$cache->save($cacheData,$handler->getID(),$handler->getGroup());
				Framework::$module->templateCache = $cacheData;

				


				// Display
				$presenter = Framework_Presenter::factory(Framework::$module->presenter, Framework::$module);
				if (!PEAR::isError($presenter)) {
					// Clear template cache
					$presenter->clearCache();
					$presenter->cacheInit();
					$presenter->display();

				} else {
					return PEAR::raiseError($presenter->getMessage(),FRAMEWORK_ERROR_PRESENTER);
				}





			}

		}

	}

	/**
	 * stop
	 *
	 * @access      public
	 * @return      mixed
	 */
	public function stop()
	{

		// Module output will be cached if it is cacheable
		$result = Framework_Module::stop(Framework::$module);
		if (PEAR::isError($result)) {
			return $result;
		}
		
		// Cache database objects
		Framework_DB::cache();
	}
}

?>
